package com.dly.blog.service.impl;

import com.dly.blog.domain.Permission;
import com.dly.blog.domain.Role;
import com.dly.blog.domain.RoleMenu;
import com.dly.blog.domain.RolePermission;
import com.dly.blog.domain.dto.MenuPermissionDTO;
import com.dly.blog.domain.vo.permission.PermissionListVO;
import com.dly.blog.mapper.PermissionMapper;
import com.dly.blog.mapper.RoleMapper;
import com.dly.blog.mapper.RolePermissionMapper;
import com.dly.blog.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dly.blog.utile.CollectionUtils;
import org.springframework.stereotype.Service;
import com.dly.blog.domain.vo.permission.PermissionVO;
import com.dly.blog.domain.ro.permission.PermissionPageRO;
import com.dly.blog.domain.ro.permission.PermissionAddRO;
import com.dly.blog.domain.ro.permission.PermissionEditRO;
import com.dly.blog.domain.vo.PageVO;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import lombok.AllArgsConstructor;
import cn.hutool.core.collection.ListUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 权限 服务实现类
 * </p>
 *
 * @author DLY
 * @since 2024-11-13 15:16:31
 */
@Service
@AllArgsConstructor
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    private final RoleMapper roleMapper;

    private final RolePermissionMapper rolePermissionMapper;

    @Override
    public PageVO<PermissionVO> getPermissionPage(PermissionPageRO permissionPageRO) {
        Page<Permission> page = baseMapper.selectPage(permissionPageRO.buildIPage(), new LambdaQueryWrapper<>());
        return PageVO.buildMyBatis(page, permission -> {
            PermissionVO permissionVO = new PermissionVO();
            BeanUtils.copyProperties(permission, permissionVO);
            return permissionVO;
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addPermission(PermissionAddRO permissionAddRO) {
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionAddRO, permission);
        baseMapper.insert(permission);


        String admin = roleMapper.selectOne(new LambdaQueryWrapper<Role>().eq(Role::getRoleCode, "admin")).getId();
        RolePermission rolePermission = new RolePermission();
        rolePermission.setRoleId(admin);
        rolePermission.setPermissionId(permission.getId());
        rolePermissionMapper.insert(rolePermission);
    }

    @Override
    public PermissionVO getPermission(String id) {
        Permission permission = baseMapper.selectById(id);
        PermissionVO permissionVO = new PermissionVO();
        BeanUtils.copyProperties(permission, permissionVO);
        return permissionVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removePermission(String[] ids) {
        baseMapper.deleteBatchIds(ListUtil.toList(ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editPermission(PermissionEditRO permissionEditRO) {
        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionEditRO, permission);
        baseMapper.updateById(permission);
    }

    @Override
    public List<PermissionListVO> getPermissionList() {
        List<PermissionListVO> permissionListVOS = new ArrayList<>();
        List<MenuPermissionDTO> menuPermissionDTOS = baseMapper.searchAllMenu();
        if(CollectionUtils.isEmpty(menuPermissionDTOS)){
            return permissionListVOS;
        }

        Map<String, List<MenuPermissionDTO>> menuPermissionMap =
                menuPermissionDTOS.stream().collect(Collectors.groupingBy(MenuPermissionDTO::getId));

        for(List<MenuPermissionDTO> menuPermissions: menuPermissionMap.values()){
            MenuPermissionDTO menuPermissionDTO = menuPermissions.get(0);

            PermissionListVO permissionListVO = new PermissionListVO();
            permissionListVO.setLabel(menuPermissionDTO.getMenuName());
            permissionListVO.setId(menuPermissionDTO.getId());
            permissionListVO.setDisabled(true);
            //获取菜单下面按钮
            permissionListVO.setChildren(menuPermissions.stream().map(menuPermissionDTO1 -> {
                PermissionListVO child = new PermissionListVO();
                child.setId(menuPermissionDTO1.getPermissionId());
                child.setCode(menuPermissionDTO1.getPermissionCode());
                child.setLabel(menuPermissionDTO1.getPermissionName());
                return child;
            }).toList());

            permissionListVOS.add(permissionListVO);
        }

        return permissionListVOS;
    }
}
